package org.example.task;

import java.util.concurrent.TimeUnit;

/**
 * 倒计时任务
 */
public class CountdownTask implements Runnable {
    private final String taskName;
    private final int seconds; // 倒计时秒数

    public CountdownTask(String taskName, int seconds) {
        this.taskName = taskName;
        this.seconds = seconds;
    }

    @Override
    public void run() {
        System.out.println("倒计时任务 [" + taskName + "] 开始，将倒计时 " + seconds + " 秒...");

        try {
            for (int i = seconds; i > 0; i--) {
                System.out.println("任务 [" + taskName + "] - 剩余时间: " + i + " 秒");
                TimeUnit.SECONDS.sleep(1);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            System.out.println("倒计时任务 [" + taskName + "] 被中断");
        }

        System.out.println("倒计时任务 [" + taskName + "] 执行完毕！");
    }
}
